﻿using System;
using System.Diagnostics.Tracing;
using System.Threading;

namespace Sustainalytics.LongRunningTaskManager.Interface
{
    [EventSource(Name = "Sustainalytics-LongRunningTaskManager")]
    public sealed class LongRunningTaskManagerEventSource : EventSource
    {

        private static Lazy<LongRunningTaskManagerEventSource> _lazyLog = new Lazy<LongRunningTaskManagerEventSource>(InitLog, LazyThreadSafetyMode.ExecutionAndPublication);

        /// <summary>
        /// Ctor.
        /// </summary>
        private LongRunningTaskManagerEventSource()
        {
        }

        public static LongRunningTaskManagerEventSource Log
        {
            get
            {
                return _lazyLog.Value;
            }
        }

        [NonEvent]
        private static LongRunningTaskManagerEventSource InitLog()
        {
            var log = new LongRunningTaskManagerEventSource();
            log.Ping();

            return log;
        }

        [Event(1, Level = EventLevel.LogAlways, Message = "LongRunningTaskManager/Ping")]
        public void Ping()
        {
            if (IsEnabled())
            {
                WriteEvent(1);
            }
        }


        #region Task
        [Event(100, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskScheduled")]
        public void TaskScheduled(LongRunningTaskTypeEnum taskType, string taskInstanceId, string startDateTime)
        {
            if (IsEnabled())
            {
                WriteEvent(100, taskType, taskInstanceId, startDateTime);
            }
        }

        [Event(101, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskRunning")]
        public void TaskRunning(LongRunningTaskTypeEnum taskType, string taskInstanceId, string startDateTime)
        {
            if (IsEnabled())
            {
                WriteEvent(101, taskType, taskInstanceId, startDateTime);
            }
        }

        [Event(102, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskCancelled")]
        public void TaskCancelled(LongRunningTaskTypeEnum taskType, string taskInstanceId, string startDateTime)
        {
            if (IsEnabled())
            {
                WriteEvent(102, taskType, taskInstanceId, startDateTime);
            }
        }

        [Event(103, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskFinished")]
        public void TaskFinished(LongRunningTaskTypeEnum taskType, string taskInstanceId, string startDateTime)
        {
            if (IsEnabled())
            {
                WriteEvent(103, taskType, taskInstanceId, startDateTime);
            }
        }

        [NonEvent]
        public void TaskFailed(LongRunningTaskTypeEnum taskType, string taskInstanceId, string startDateTime, Exception ex)
        {
            if (IsEnabled())
            {
                TaskFailed(taskType, taskInstanceId, startDateTime, ex.ToString());
            }
        }

        [Event(104, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskFailed")]
        public void TaskFailed(LongRunningTaskTypeEnum taskType, string taskInstanceId, string startDateTime, string err)
        {
            if (IsEnabled())
            {
                WriteEvent(104, taskType, taskInstanceId, startDateTime, err);
            }
        }
        #endregion


        #region TaskManager

        [Event(200, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerInit")]
        public void TaskManagerInit()
        {
            if (IsEnabled())
            {
                WriteEvent(200);
            }
        }

        [Event(201, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerInitSuccess")]
        public void TaskManagerInitSuccess()
        {
            if (IsEnabled())
            {
                WriteEvent(201);
            }
        }

        [NonEvent]
        public void TaskManagerInitError(Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerInitError(ex.ToString());
            }
        }

        [Event(202, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskManagerInitError")]
        public void TaskManagerInitError(string err)
        {
            if (IsEnabled())
            {
                WriteEvent(202, err);
            }
        }


        [Event(300, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerStopping")]
        public void TaskManagerStopping()
        {
            if (IsEnabled())
            {
                WriteEvent(200);
            }
        }

        [Event(301, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerStoppingSuccess")]
        public void TaskManagerStoppingSuccess()
        {
            if (IsEnabled())
            {
                WriteEvent(301);
            }
        }

        [NonEvent]
        public void TaskManagerStoppingError(Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerStoppingError(ex.ToString());
            }
        }

        [Event(302, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskManagerStoppingError")]
        public void TaskManagerStoppingError(string err)
        {
            if (IsEnabled())
            {
                WriteEvent(302, err);
            }
        }

        [Event(303, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerStopped")]
        public void TaskManagerStopped()
        {
            if (IsEnabled())
            {
                WriteEvent(303);
            }
        }


        [Event(400, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerStartTask")]
        public void TaskManagerStartTask(string title, string username, string paramJson, LongRunningTaskTypeEnum taskType)
        {
            if (IsEnabled())
            {
                WriteEvent(400, title, username, paramJson, taskType);
            }
        }

        [Event(401, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerStartTaskSuccess")]
        public void TaskManagerStartTaskSuccess(string title, string username, string paramJson, LongRunningTaskTypeEnum taskType)
        {
            if (IsEnabled())
            {
                WriteEvent(401, title, username, paramJson, taskType);
            }
        }

        [NonEvent]
        public void TaskManagerStartTaskError(string title, string username, string paramJson, LongRunningTaskTypeEnum taskType, Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerStartTaskError(title, username, paramJson, taskType, ex.ToString());
            }
        }

        [Event(402, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskManagerStartTaskError")]
        public void TaskManagerStartTaskError(string title, string username, string paramJson, LongRunningTaskTypeEnum taskType, string err)
        {
            if (IsEnabled())
            {
                WriteEvent(402, title, username, paramJson, taskType, err);
            }
        }


        [Event(500, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerGetOverallStatus")]
        public void TaskManagerGetOverallStatus(string username)
        {
            if (IsEnabled())
            {
                WriteEvent(500, username);
            }
        }

        [Event(501, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerGetOverallStatusSuccess")]
        public void TaskManagerGetOverallStatusSuccess(string username)
        {
            if (IsEnabled())
            {
                WriteEvent(501, username);
            }
        }

        [NonEvent]
        public void TaskManagerGetOverallStatusError(string username, Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerGetOverallStatusError(username, ex.ToString());
            }
        }

        [Event(502, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskManagerGetOverallStatusError")]
        public void TaskManagerGetOverallStatusError(string username, string err)
        {
            if (IsEnabled())
            {
                WriteEvent(502, username, err);
            }
        }


        [Event(600, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerGetStatus")]
        public void TaskManagerGetStatus(string username, string taskInstanceId)
        {
            if (IsEnabled())
            {
                WriteEvent(600, username, taskInstanceId);
            }
        }

        [Event(601, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerGetStatusSuccess")]
        public void TaskManagerGetStatusSuccess(string username, string taskInstanceId)
        {
            if (IsEnabled())
            {
                WriteEvent(601, username, taskInstanceId);
            }
        }

        [NonEvent]
        public void TaskManagerGetStatusError(string username, string taskInstanceId, Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerGetStatusError(username, taskInstanceId, ex.ToString());
            }
        }

        [Event(602, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerGetStatusError")]
        public void TaskManagerGetStatusError(string username, string taskInstanceId, string err)
        {
            if (IsEnabled())
            {
                WriteEvent(602, username, taskInstanceId, err);
            }
        }


        [Event(700, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerGetResult")]
        public void TaskManagerGetResult(string username, string taskInstanceId)
        {
            if (IsEnabled())
            {
                WriteEvent(700, username, taskInstanceId);
            }
        }

        [Event(701, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerGetResultSuccess")]
        public void TaskManagerGetResultSuccess(string username, string taskInstanceId)
        {
            if (IsEnabled())
            {
                WriteEvent(701, username, taskInstanceId);
            }
        }

        [NonEvent]
        public void TaskManagerGetResultError(string username, string taskInstanceId, Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerGetResultError(username, taskInstanceId, ex.ToString());
            }
        }

        [Event(702, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskManagerGetResultError")]
        public void TaskManagerGetResultError(string username, string taskInstanceId, string err)
        {
            if (IsEnabled())
            {
                WriteEvent(702, username, taskInstanceId, err);
            }
        }


        [Event(800, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerCancel")]
        public void TaskManagerCancel(string username, string taskInstanceId)
        {
            if (IsEnabled())
            {
                WriteEvent(800, username, taskInstanceId);
            }
        }

        [Event(801, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerCancelSuccess")]
        public void TaskManagerCancelSuccess(string username, string taskInstanceId)
        {
            if (IsEnabled())
            {
                WriteEvent(801, username, taskInstanceId);
            }
        }

        [NonEvent]
        public void TaskManagerCancelError(string username, string taskInstanceId, Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerCancelError(username, taskInstanceId, ex.ToString());
            }
        }

        [Event(802, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskManagerCancelError")]
        public void TaskManagerCancelError(string username, string taskInstanceId, string err)
        {
            if (IsEnabled())
            {
                WriteEvent(802, username, taskInstanceId, err);
            }
        }


        [Event(900, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerRemove")]
        public void TaskManagerRemove(string username, string taskInstanceId)
        {
            if (IsEnabled())
            {
                WriteEvent(900, username, taskInstanceId);
            }
        }

        [Event(901, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerRemoveSuccess")]
        public void TaskManagerRemoveSuccess(string username, string taskInstanceId)
        {
            if (IsEnabled())
            {
                WriteEvent(901, username, taskInstanceId);
            }
        }

        [NonEvent]
        public void TaskManagerRemoveError(string username, string taskInstanceId, Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerRemoveError(username, taskInstanceId, ex.ToString());
            }
        }

        [Event(902, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskManagerRemoveError")]
        public void TaskManagerRemoveError(string username, string taskInstanceId, string err)
        {
            if (IsEnabled())
            {
                WriteEvent(902, username, taskInstanceId, err);
            }
        }
        #endregion


        #region TaskManagerFactory
        [Event(1000, Level = EventLevel.Informational, Message = "LongRunningTaskManager/TaskManagerFactoryTaskAdded")]
        public void TaskManagerFactoryTaskAdded(string taskType)
        {
            if (IsEnabled())
            {
                WriteEvent(1000, taskType);
            }
        }

        [NonEvent]
        public void TaskManagerFactoryTaskAddError(string taskType, Exception ex)
        {
            if (IsEnabled())
            {
                TaskManagerFactoryTaskAddError(taskType, ex.ToString());
            }
        }

        [Event(1001, Level = EventLevel.Error, Message = "LongRunningTaskManager/TaskManagerFactoryTaskAddError")]
        public void TaskManagerFactoryTaskAddError(string taskType, string err)
        {
            if (IsEnabled())
            {
                WriteEvent(1001, taskType, err);
            }
        }
        #endregion
    }
}
